# Contributor: Michał Polański <michal@polanski.me>
# Maintainer: Michał Polański <michal@polanski.me>
pkgname=himalaya
pkgver=0.9.0
pkgrel=0
pkgdesc="CLI email client"
url="https://github.com/soywod/himalaya"
license="MIT"
arch="all !ppc64le !riscv64 !s390x" # fails to build ring crate
makedepends="cargo cargo-auditable bzip2-dev sqlite-dev zstd-dev"
subpackages="
	$pkgname-doc
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="https://github.com/soywod/himalaya/archive/v$pkgver/himalaya-$pkgver.tar.gz
	cargo-update-libc-lfs64.patch
	"
options="net" # fetch dependencies

_features="--no-default-features --features imap-backend,smtp-sender,pgp-commands"


prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked
	mkdir man-pages

	# Rust target triple
	local target=$(rustc -vV | sed -n 's/host: //p')

	# build against system-provided libs
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		bzip2 = { rustc-link-lib = ["bz2"] }
		zstd = { rustc-link-lib = ["zstd"] }
	EOF
}

build() {
	cargo auditable build --release --frozen $_features

	./target/release/himalaya man man-pages

	./target/release/himalaya completion bash > $pkgname.bash
	./target/release/himalaya completion fish > $pkgname.fish
	./target/release/himalaya completion zsh > $pkgname.zsh
}

check() {
	# Increase stack size to avoid stack overflow while compiling 'p384'.
	export RUST_MIN_STACK="4194304"

	# skip imap backend e2e test
	cargo test --frozen $_features -- \
		--skip test_imap_backend
}

package() {
	install -Dm755 target/release/$pkgname -t "$pkgdir"/usr/bin/

	install -Dm644 man-pages/*.1 -t "$pkgdir"/usr/share/man/man1/

	install -Dm644 $pkgname.bash "$pkgdir"/usr/share/bash-completion/completions/$pkgname
	install -Dm644 $pkgname.fish "$pkgdir"/usr/share/fish/vendor_completions.d/$pkgname.fish
	install -Dm644 $pkgname.zsh "$pkgdir"/usr/share/zsh/site-functions/_$pkgname
}

sha512sums="
0233289968a80672e39d7cd133f14bb9cd27129f389fccc4b366844389395a5bbfb89184e4d2b2ef2ecbbcaaf959a679f937f4b4b7c6018a52269a196c4af551  himalaya-0.9.0.tar.gz
a07efe929420577315ce1c66f065f72f2574ad863531192c70980e0cfc586af151aafb417adc8c409a549478e5284c0cb6fb7289c95b520ade13507ceab750a4  cargo-update-libc-lfs64.patch
"
